<template>
  <div class="categories">
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片式图 -->
    <el-card>
      <!-- 添加分类按钮 -->
      <el-row>
        <el-col>
          <el-button type="primary" @click="showAddCategory">
            添加分类
          </el-button>
        </el-col>
      </el-row>

      <!-- 表格内容 -->
      <tree-table
        class="cate_list"
        :data="cateList"
        :columns="columns"
        :selection-type="false"
        :expand-type="false"
        show-index
        border
        :show-row-hover="false"
      >
        <template slot="isdeleted" slot-scope="scope">
          <i class="el-icon-error" v-if="scope.row.cat_deleted"></i>
          <i class="el-icon-success" v-else></i>
        </template>
        <template slot="order" slot-scope="scope">
          <el-tag size="mini" v-if="scope.row.cat_level == 0">一级</el-tag>
          <el-tag
            size="mini"
            type="success"
            v-else-if="scope.row.cat_level == 1"
            >二级</el-tag
          >
          <el-tag size="mini" type="warning" v-else>三级</el-tag>
        </template>
        <template slot="opt" slot-scope="scope">
          <el-button type="primary" icon="el-icon-edit" size="mini"
            >编辑</el-button
          >
          <el-button type="danger" icon="el-icon-delete" size="mini"
            >删除</el-button
          >
        </template>
      </tree-table>

      <!-- 分页部分 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[1, 2, 5, 10]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>

    <!-- 添加商品分类对话框 -->
    <el-dialog
      title="添加分类"
      :visible.sync="addcategoryVisible"
      width="50%"
      @close="addCateDialogClosed"
    >
      <el-form :model="addCategoryForm" :rules="rules" ref="addCategoryForm">
        <el-form-item
          label="分类名称："
          :label-width="formLabelWidth"
          prop="cat_name"
        >
          <el-input
            v-model="addCategoryForm.cat_name"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="父级分类：" :label-width="formLabelWidth">
          <el-cascader
            v-model="selectedKeys"
            :options="parentCateList"
            :props="cascaderProps"
            @change="parentCateChange"
            clearable
          ></el-cascader>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="addcategoryVisible = false">取 消</el-button>
        <el-button type="primary" @click="addCategory">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      cateList: [], //商品分类的数据列表，默认为空
      //查询条件
      queryInfo: {
        type: 3,
        pagenum: 1,
        pagesize: 5,
      },
      total: 0, //总数据条数
      //为table指定列的定义
      columns: [
        {
          label: "分类名称",
          prop: "cat_name",
        },
        {
          label: "是否有效",
          prop: "cat_deleted",
          type: "template", //表示，将当前列定义为模板列
          template: "isdeleted", //表示当前这一列使用模板名称
        },
        {
          label: "排序",
          prop: "cat_level",
          type: "template", //表示，将当前列定义为模板列
          template: "order", //表示当前这一列使用模板名称
        },
        {
          label: "操作",
          type: "template", //表示，将当前列定义为模板列
          template: "opt", //表示当前这一列使用模板名称
        },
      ],
      addcategoryVisible: false, //控制添加分类的对话框的显示隐藏
      formLabelWidth: "100px", //控制弹窗的宽度
      //添加分类的表单数据对象
      addCategoryForm: {
        cat_pid: 0,
        cat_name: "",
        cat_level: 0,
      },
      parentCateList: [], //父级分类的列表
      cascaderProps: {
        expandTrigger: "hover",
        checkStrictly: "true",
        value: "cat_id",
        label: "cat_name",
        children: "children",
      },
      selectedKeys: [],
      rules: {
        cat_name: [
          { required: true, message: "请输入用户名", trigger: "blur" },
        ],
      },
    };
  },
  mounted() {
    this.getCateList();
  },
  methods: {
    //获取商品分类数据列表
    getCateList() {
      this.axios
        .get("categories", {
          params: this.queryInfo,
        })
        .then((res) => {
          let result = res.data;
          if (result.meta.status !== 200) {
            return this.$message.error("获取商品分类失败");
          }
          // console.log(result.data.result);
          this.cateList = result.data.result;
          this.total = result.data.total;
        });
    },
    //分页功能函数
    //监听pagesize改变的事件
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize;
      this.getCateList();
    },
    //监听pagenum改变的事件
    handleCurrentChange(newNum) {
      this.queryInfo.pagenum = newNum;
      this.getCateList();
    },
    // 展示添加分类对话框
    showAddCategory() {
      this.axios
        .get("categories", {
          params: { type: 2 },
        })
        .then((res) => {
          let result = res.data;
          if (result.meta.status !== 200) {
            return this.$message.error("获取商品分类失败");
          }
          this.parentCateList = result.data;
        });
      this.addcategoryVisible = true;
    },
    parentCateChange() {
      // console.log(this.selectedKeys);
      //如果selectedKeys数组中的length大于0 证明选中了父级分类
      //没有选中任何父级分类
      if (this.selectedKeys.length > 0) {
        //获取父级分类id
        this.addCategoryForm.cat_pid =
          this.selectedKeys[this.selectedKeys.length - 1];
        //为当前分类的等级赋值
        this.addCategoryForm.cat_level = this.selectedKeys.length;
        return;
      } else {
        this.addCategoryForm.cat_pid = 0;
        this.addCategoryForm.cat_level = 0;
      }
    },
    //点击添加新的分类
    addCategory() {
      console.log(this.addCategoryForm);
      this.$refs.addCategoryForm.validate(valid=>{
        if (valid) {
          this.axios.post("categories",this.addCategoryForm).then(res=>{
            let result = res.data
            console.log(result);
            if (result.meta.statsu !== 201) {
              return this.$message.error("添加分类失败")
            }
            this.addcategoryVisible = false
            this.getCateList()
          })
        }
      })
    },
    //监听添加分类对话框关闭
    addCateDialogClosed() {
      this.$refs.addCategoryForm.resetFields();
      this.selectedKeys = [];
      this.addCategoryForm.cat_pid = 0;
      this.addCategoryForm.cat_level = 0;
    },
  },
};
</script>

<style lang="scss" scoped>
.categories {
  .el-icon-success {
    color: lightgreen;
  }
  .el-icon-error {
    color: red;
  }
  .el-pagination {
    margin-top: 15px;
    text-align: left;
  }
  .cate_list {
    margin-top: 15px;
  }
  .el-cascader {
    width: 100%;
  }
  .el-col {
    text-align: left;
  }
}
</style>